from mcpy.decorators import reflectable, method
from .module import NetworkModule, protocol_type, connection_status
from dataclasses import field


@reflectable("NetworkClient", module=NetworkModule)
class NetworkClient:
    _host: str = field(default="", name="Host")
    _port: int = field(default=0, name="Port")
    _status: connection_status = field(
        default=connection_status.DISCONNECTED, name="Status"
    )
    _last_sent: str = field(default="", name="LastSent")
    _proto: protocol_type = field(default=protocol_type.TCP, name="Protocol")

    def __init__(self) -> None:
        self._host = ""
        self._port = 0
        self._status: connection_status = connection_status.DISCONNECTED
        self._last_sent = ""

    @method(name="Connect")
    def connect(self, host: str, port: int) -> bool:
        self._host = host
        self._port = port
        self._status = connection_status.CONNECTED
        return True

    @method(name="Disconnect")
    def disconnect(self) -> None:
        self._status = connection_status.DISCONNECTED
        self._host = ""
        self._port = 0

    @method(name="SendData")
    def send_data(self, data: str) -> bool:
        if self._status != connection_status.CONNECTED:
            return False
        self._last_sent = data
        return True

    @method(name="ReceiveData")
    def receive_data(self) -> str:
        if self._status != connection_status.CONNECTED:
            return ""
        return "Echo: " + self._last_sent

    @method(name="GetStatus")
    def get_status(self) -> connection_status:
        return self._status

    @method(name="GetHost")
    def get_host(self) -> str:
        return self._host

    @method(name="GetPort")
    def get_port(self) -> int:
        return self._port

    @method(name="SetProtocol")
    def set_protocol(self, proto: protocol_type) -> None:
        self._proto = proto

    @method(name="GetProtocol")
    def get_protocol(self) -> protocol_type:
        return self._proto
